﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using IronOne.SalesOne.BusinessObjects;

namespace IronOne.SalesOne.DataObjects
{
    public class LinqUserRole_aspnet
    {
        public List<UserRole_aspnet> GetAsp_netUserRoles(string sortExpression)
        {
            using (SODataDataContext db = DataContextFactory.CreateContext())
            {
                IQueryable<aspnet_RoleEntity> query = db.aspnet_RoleEntities;

                if (sortExpression.Length > 0)
                {
                    string[] sort = sortExpression.Split(' ');
                    string sortColumn = sort[0];
                    string sortOrder = sort[1];

                    switch (sortColumn)
                    {
                        case "RoleId":
                            if (sortOrder == "ASC")
                                query = query.OrderBy(c => c.RoleId);
                            else
                                query = query.OrderByDescending(c => c.RoleId);
                            break;
                        case "RoleName":
                            if (sortOrder == "ASC")
                                query = query.OrderBy(c => c.RoleName);
                            else
                                query = query.OrderByDescending(c => c.RoleName);
                            break;
                    }
                }
                return query.Select(c => Mapper.ToAsp_netUserRoleObject(c)).ToList();
            }
        }

        public List<UserRole_aspnet> GetAsp_netUserRolesForUserType(int UserTypeId)
        {
            using (SODataDataContext db = DataContextFactory.CreateContext())
            {
                IQueryable<aspnet_RoleEntity> query = db.aspnet_RoleEntities;
                //return query.Select(c => Mapper.ToUserRoleObject(c => c.UserTypeId==UserTypeId)).ToList();
                query = from p in db.aspnet_RoleEntities
                        join riu in db.RolesInUserTypeEntities
                        on p.RoleId
                        equals riu.RoleId
                        where riu.UserTypeId== UserTypeId
                        select p;
                return query.Select(c => Mapper.ToAsp_netUserRoleObject(c)).ToList();
                
            }
            return null;
        }

        public UserRole_aspnet GetAsp_netUserRole(string roleName)
        {
            using (SODataDataContext db = DataContextFactory.CreateContext())
            {
                return Mapper.ToAsp_netUserRoleObject(db.aspnet_RoleEntities
                            .SingleOrDefault(p => p.RoleName== roleName));
            }
        }

        public UserRole_aspnet GetAsp_netUserRole(Guid roleId)
        {
            using (SODataDataContext db = DataContextFactory.CreateContext())
            {
                return Mapper.ToAsp_netUserRoleObject(db.aspnet_RoleEntities
                            .SingleOrDefault(p => p.RoleId  == roleId));
            }
        }

        //public void InsertUser(UserRole_aspnet userRole_aspnet)
        //{
        //    using (SODataDataContext db = DataContextFactory.CreateContext())
        //    {
        //        try
        //        {
        //            UserRole_aspnetEntity entity = Mapper.ToUserEntity(userRole_aspnet);
        //            db.UserEntities.InsertOnSubmit(entity);
        //            db.SubmitChanges();

        //            userRole_aspnet.UserId = entity.UserId;
        //        }
        //        catch (ChangeConflictException)
        //        {
        //            throw new Exception("A change to contact record was made before your changes.");
        //        }
        //        catch (Exception)
        //        {
        //            throw new Exception("Unable to Insert contact " + userRole_aspnet.Name);
        //        }
        //    }
        //}

        public int UpdateAsp_netUserRole(UserRole_aspnet userRole_aspnet)
        {
            using (SODataDataContext db = DataContextFactory.CreateContext())
            {
                try
                {
                    aspnet_RoleEntity entity = Mapper.ToAsp_netRoleEntity(userRole_aspnet);

                    db.aspnet_RoleEntities.Attach(entity, true);
                    db.SubmitChanges();
                    return 1;
                }
                catch (ChangeConflictException)
                {
                    throw new Exception("A change to contact record was made before your changes.");
                }
                catch (Exception)
                {
                    return 0;
                }
            }
        }

        public int DeleteAsp_netUserRole(UserRole_aspnet userRole_aspnet)
        {
            using (SODataDataContext db = DataContextFactory.CreateContext())
            {
                try
                {
                    aspnet_RoleEntity entity = Mapper.ToAsp_netRoleEntity(userRole_aspnet);
                    db.aspnet_RoleEntities.Attach(entity, false);
                    db.aspnet_RoleEntities.DeleteOnSubmit(entity);
                    db.SubmitChanges();

                    return 1;
                }
                catch (ChangeConflictException)
                {
                    throw new Exception("A change to contact record was made before your changes.");
                }
                catch (Exception)
                {
                    return 0;
                }
            }
        }

        public List<UserRole_aspnet> GetRolesForUserTypes(int[] userTypes)
        {
            using (SODataDataContext db = DataContextFactory.CreateContext())
            {
                IQueryable<aspnet_RoleEntity> query = db.aspnet_RoleEntities;

                query = from p
                        in db.aspnet_RoleEntities
                        where userTypes.Contains(p.RolesInUserTypeEntity.UserTypeId)
                        select p;

                return query.Select(c => Mapper.ToAsp_netUserRoleObject(c)).ToList();
            }
            return null;
        }
    }
}
